Ext.define('YX.core.ModuleFunctionSetup', {
	extend : 'Ext.panel.Panel',
	gridFunList : null,
	gridFunCtlList : null,
	txtSearchFunCode : null,
	txtSearchFunDesc : null,
	cmbSearchGroup : null,
	txtFunCode : null,
	txtFunDesc : null,
	txtAsmName : null,
	txtAsmFile : null,
	txtAsmClassName : null,
	cmbGroup : null,
	cmbFunSide : null,
	frmOperate : null,
	txtFunCtlName : null,
	txtFunCtlDesc : null,
	txtFunServiceName : null,
	btnSearch : null,
	btnSave : null,
	btnDelete : null,
	btnReset : null,
	btnAddCtl : null,
	btnRemoveCtl : null,
	layout : {
		type : 'border',
		align : 'stretch'
	},
	initComponent : function() {
		this.gridFunList = Ext.create('Ext.grid.Panel', {
			  title : T.label.moduleFunctionList,
			  flex : 1,
			  columns : [{
				    text : T.label.code,
				    dataIndex : 'funCode'
			    }, {
				    text : T.label.desc + "1",
				    dataIndex : 'funDesc',
				    width : 200
			    }, {
				    text : T.label.desc + "2",
				    dataIndex : 'localeFunDesc',
				    width : 200
			    }, {
				    text : T.label.type,
				    dataIndex : 'funType',
				    width : 60,
				    renderer : function(value) {
					    return 'F' === value ? T.label.func : T.label.menu;
				    }
			    }, {
				    text : T.label.asmName,
				    dataIndex : 'asmName',
				    width : 150
			    }, {
				    text : T.label.file,
				    dataIndex : 'asmFile',
				    width : 200
			    }, {
				    text : T.label.viewClassName,
				    dataIndex : 'asmClassName',
				    width : 100
			    }, {
				    text : T.label.group,
				    dataIndex : 'funGroup',
				    width : 60,
				    value : 'EXT',
				    renderer : function(value) {
					    if (value === 'QT') {
						    return 'Qt';
					    } else if (value === 'EXT') {
						    return 'Extjs';
					    }

					    return value;
				    }
			    }, {
				    text : T.label.clientSide,
				    dataIndex : 'funSide',
				    width : 60,
				    renderer : function(value) {
					    if (value === 'U') {
						    return T.label.func;
					    } else if (value === 'O') {
						    return T.label.operate;
					    } else if (value === 'C') {
						    return T.label.chart;
					    }

					    return value;
				    }
			    }],
			  store : Ext.create('Ext.data.JsonStore', {
				    fields : ['funCode', 'funDesc', 'localeFunDesc', 'funType', 'asmName',
				      'asmFile', 'asmClassName', 'funGroup', 'funSide'],
				    pageSize : 15,
				    proxy : {
					    type : 'zbusrpc',
					    module : 'BasViewModuleFunctionDefinitionService',
					    extraParams : {
						    funGroup : 'EXT'
					    },
					    reader : {
						    root : 'funDefList'
					    }
				    }
			    }),
			  listeners : {
				  scope : this,
				  select : this.onGridFunListSelect
			  }
		  });

		// 查询的组件
		this.txtSearchFunCode = Ext.create('Ext.form.field.Text', {
			  fieldLabel : T.label.code,
			  labelWidth : 60
		  });
		this.txtSearchFunDesc = Ext.create('Ext.form.field.Text', {
			  fieldLabel : T.label.desc,
			  labelWidth : 60
		  });
		this.cmbSearchGroup = Ext.create('Ext.form.field.ComboBox', {
			  fieldLabel : T.label.group,
			  labelWidth : 60,
			  valueField : 'value',
			  displayField : 'text',
			  value : 'EXT',
			  store : Ext.create('Ext.data.ArrayStore', {
				    fields : ['value', 'text'],
				    data : [['QT', 'QT'], ['EXT', 'Extjs']]
			    })
		  });
		// 操作的组件
		this.txtFunCode = Ext.create('Ext.form.field.Text', {
			  fieldLabel : T.label.code,
			  labelWidth : 60,
			  name : 'funCode',
			  allowBlank : false,
			  maxLength : 20,
			  listeners : {
				  scope : this,
				  blur : this.onTxtFunCodeBlur
			  }
		  });
		this.txtFunDesc = Ext.create('Ext.form.field.Text', {
			  fieldLabel : T.label.desc,
			  name : 'funDesc',
			  labelWidth : 60,
			  maxLength : 100
		  });
		this.txtAsmName = Ext.create('Ext.form.field.Text', {
			  fieldLabel : T.label.asmName,
			  name : 'asmName',
			  labelWidth : 60
		  });
		this.txtAsmFile = Ext.create('Ext.form.field.Text', {
			  fieldLabel : T.label.file,
			  name : 'asmFile',
			  labelWidth : 60
		  });
		this.txtAsmClassName = Ext.create('Ext.form.field.Text', {
			  fieldLabel : T.label.viewClassName,
			  name : 'asmClassName',
			  labelWidth : 60
		  });
		this.cmbGroup = Ext.create('Ext.form.field.ComboBox', {
			  fieldLabel : T.label.group,
			  editable : false,
			  name : 'funGroup',
			  labelWidth : 60,
			  valueField : 'value',
			  displayField : 'text',
			  value : 'EXT',
			  store : Ext.create('Ext.data.ArrayStore', {
				    fields : ['value', 'text'],
				    data : [['QT', 'Qt'], ['EXT', 'Extjs']]
			    })
		  });
		this.cmbFunSide = Ext.create('Ext.form.field.ComboBox', {
			fieldLabel : T.label.clientSide,
			editable : false,
			name : 'funSide',
			labelWidth : 60,
			valueField : 'value',
			displayField : 'text',
			value : 'U',
			store : Ext.create('Ext.data.ArrayStore', {
				  fields : ['value', 'text'],
				  data : [['U', T.label.func], ['O', T.label.operate], ['C', T.label.chart]]
			  })
		});
		this.frmOperate = Ext.create('Ext.form.Panel', {
			  flex : 1,
			  defaults : {
				  anchor : '98%'
			  },
			  title : T.label.operate,
			  items : [this.txtFunCode, this.txtFunDesc, this.txtAsmName,
			    this.txtAsmFile, this.txtAsmClassName, this.cmbGroup, this.cmbFunSide]
		  });

		this.btnSave = Ext.create('Ext.button.Button', {
			  text : T.label.save,
			  itemId : 'btnSave',
			  iconCls : 'icon-disk',
			  scope : this,
			  handler : this.onBtnSaveClicked
		  });
		this.btnDelete = Ext.create('Ext.button.Button', {
			  text : T.label.del,
			  itemId : 'btnDelete',
			  iconCls : 'icon-delete',
			  scope : this,
			  handler : this.onBtnDeleteClicked
		  });
		this.btnSearch = Ext.create('Ext.button.Button', {
			  text : T.label.search,
			  itemId : 'btnSearch',
			  iconCls : 'icon-find',
			  scope : this,
			  handler : this.onBtnSearchClicked
		  });
		this.btnReset = Ext.create('Ext.button.Button', {
			  text : T.label.reset,
			  itemId : 'btnReset',
			  iconCls : 'icon-clear',
			  scope : this,
			  handler : this.onBtnResetClicked
		  });

		// 控制项
		this.gridFunCtlList = Ext.create('Ext.grid.Panel', {
			  title : T.label.controlList,
			  flex : 1,
			  columns : [{
				    text : T.label.controlName,
				    dataIndex : 'ctlName'
			    }, {
				    text : T.label.controlDesc,
				    dataIndex : 'ctlDesc',
				    width : 200
			    }, {
				    text : T.label.serviceName,
				    dataIndex : 'serviceName',
				    width : 200
			    }],
			  store : Ext.create('Ext.data.JsonStore', {
				    fields : ['ctlName', 'ctlDesc', 'serviceName'],
				    proxy : {
					    type : 'zbusrpc',
					    module : 'BasViewModuleFunctionControlService',
					    reader : {
						    root : 'funCtlList'
					    }
				    }
			    })
		  });

		// 控制项新增
		this.txtFunCtlName = Ext.create('Ext.form.field.Text', {
			  fieldLabel : T.label.controlName,
			  labelWidth : 80,
			  allowBlank : false,
			  maxLength : 30,
			  anchor : '98%'
		  });

		this.txtFunCtlDesc = Ext.create('Ext.form.field.Text', {
			  fieldLabel : T.label.controlDesc,
			  labelWidth : 80,
			  anchor : '98%'
		  });
		this.txtFunServiceName = Ext.create('Ext.form.field.Text', {
			  fieldLabel : T.label.serviceName,
			  labelWidth : 80,
			  anchor : '98%'
		  });

		// 控制项按钮
		this.btnAddCtl = Ext.create('Ext.button.Button', {
			  iconCls : 'icon-go-left',
			  scope : this,
			  handler : this.onBtnAddCtlClicked
		  });
		this.btnRemoveCtl = Ext.create('Ext.button.Button', {
			  iconCls : 'icon-go-right',
			  scope : this,
			  handler : this.onBtnRemoveCtlClicked
		  });

		this.items = [{
			region : 'center',
			layout : {
				type : 'vbox',
				align : 'stretch'
			},
			items : [{
				  xtype : 'panel',
				  region : 'center',
				  flex : 1,
				  layout : {
					  type : 'vbox',
					  align : 'stretch'
				  },
				  items : [this.gridFunList, {
					    xtype : 'pagingtoolbar',
					    displayInfo : true,
					    store : this.gridFunList.getStore()
				    }]
			  }, {
				  height : 300,
				  layout : {
					  type : 'hbox',
					  align : 'stretch'
				  },
				  items : [this.gridFunCtlList, {
					    xtype : 'toolbar',
					    layout : {
						    type : 'vbox',
						    pack : 'center'
					    },
					    items : [this.btnAddCtl, this.btnRemoveCtl]
				    }, {
					    width : 300,
					    xtype : 'form',
					    items : [this.txtFunCtlName, this.txtFunCtlDesc,
					      this.txtFunServiceName]
				    }]
			  }]
		}, {
			region : 'east',
			width : 300,
			split : true,
			layout : {
				type : 'vbox',
				align : 'stretch'
			},
			items : [{
				  xtype : 'form',
				  defaults : {
					  anchor : '98%'
				  },
				  title : T.label.Search,
				  items : [this.txtSearchFunCode, this.txtSearchFunDesc,
				    this.cmbSearchGroup]
			  }, this.frmOperate, {
				  xtype : 'toolbar',
				  items : [this.btnSave, this.btnDelete, this.btnSearch, this.btnReset]
			  }]
		}]

		this.callParent();
	},
	onBtnSaveClicked : function(btn) {
		if (!this.frmOperate.getForm().isValid()) {
			return;
		}
		var frmVals = this.frmOperate.getValues();
		if (!Ext.isEmpty(frmVals.asmFile) && !Ext.isEmpty(frmVals.asmClassName)) {
			Ext.Msg.alert('error', T.file_and_class_name_only_one_can_accepted);
			return;
		}

		if (!Ext.isEmpty(frmVals.asmClassName)
		  && frmVals.asmClassName.indexOf('.view.') <= 0
		  && frmVals.asmClassName.split('_').length != 2) {
			Ext.Msg.alert('error', T.class_name_is_invalid);
			return;
		}

		Ext.Msg.show({
			  title : T.label.save,
			  msg : T.msg.are_you_sure_to_save_data,
			  icon : Ext.Msg.QUESTION,
			  buttons : Ext.Msg.YESNO,
			  scope : this,
			  fn : function(btnId) {
				  if (btnId == 'yes') {
					  btn.disable();
					  this.saveFun(btn);
				  }
			  }
		  });
	},
	saveFun : function(btn) {
		var frmVals = this.frmOperate.getValues();
		frmVals.funCtlList = [];
		var ctlStore = this.gridFunCtlList.getStore();
		ctlStore.each(function(rcd) {
			  frmVals.funCtlList.push({
				    funCode : frmVals.funCode,
				    ctlName : rcd.get('ctlName'),
				    ctlDesc : rcd.get('ctlDesc'),
				    serviceName : rcd.get('serviceName')
			    });
		  });

		YX.ZbusRpc.request({
			  module : 'BasSaveModuleFunctionDefinitionService',
			  scope : this,
			  showSuccess : true,
			  params : frmVals,
			  callback : function() {
				  btn.enable();
			  },
			  success : function() {
				  this.gridFunList.getStore().loadPage(1);
			  }
		  });
	},
	onBtnDeleteClicked : function() {
		if (Ext.isEmpty(this.gridFunList.getSelectionModel().getSelection())) {
			return;
		}

		Ext.Msg.show({
			  title : T.label.del,
			  msg : T.msg.are_you_sure_to_delete_data,
			  icon : Ext.Msg.QUESTION,
			  buttons : Ext.Msg.YESNO,
			  scope : this,
			  fn : function(btnId) {
				  if (btnId == 'yes') {
					  btn.disable();
					  this.deleteFun(btn);
				  }
			  }
		  });
	},
	deleteFun : function(btn) {
		var rcds = this.gridFunList.getSelectionModel().getSelection();
		YX.ZbusRpc.request({
			  module : 'BasDeleteModuleFunctionDefinitionService',
			  scope : this,
			  showSuccess : true,
			  params : {
				  funCode : rcds[0].get('funCode')
			  },
			  callback : function() {
				  btn.enable();
			  },
			  success : function() {
				  this.gridFunList.getStore().loadPage(1);
			  }
		  });
	},
	onBtnResetClicked : function() {
		this.frmOperate.getForm().reset();
	},
	onBtnSearchClicked : function() {
		var funProxy = this.gridFunList.getStore().getProxy();
		funProxy.setExtraParam('funCode', this.txtSearchFunCode.getValue());
		funProxy.setExtraParam('funDesc', this.txtSearchFunDesc.getValue());
		funProxy.setExtraParam('funGroup', this.cmbSearchGroup.getValue());
		this.gridFunList.getStore().loadPage(1);
	},
	onTxtFunCodeBlur : function(field) {
		this.gridFunCtlList.getStore().load({
			  params : {
				  funCode : field.getValue()
			  }
		  });
	},
	onGridFunListSelect : function(grid, record, index) {
		this.frmOperate.loadRecord(record);

		this.gridFunCtlList.getStore().load({
			  params : {
				  funCode : record.get('funCode')
			  }
		  });
	},
	onBtnAddCtlClicked : function(btn) {
		if (this.txtFunCtlName.isValid()) {
			var index = this.gridFunCtlList.getStore().findExact('ctlName',
			  this.txtFunCtlName.getValue());
			if (index >= 0) {
				this.gridFunCtlList.getStore().removeAt(index);
			}

			this.gridFunCtlList.getStore().insert(0, {
				  ctlName : this.txtFunCtlName.getValue(),
				  ctlDesc : this.txtFunCtlDesc.getValue(),
				  serviceName : this.txtFunServiceName.getValue()
			  });

			this.txtFunCtlName.setValue('');
			this.txtFunCtlDesc.setValue('');
			this.txtFunServiceName.setValue('');
		}
	},
	onBtnRemoveCtlClicked : function(btn) {
		var rcds = this.gridFunCtlList.getSelectionModel().getSelection();
		if (!Ext.isEmpty(rcds)) {
			this.gridFunCtlList.getStore().remove(rcds);
		}
	}
});
